<template>
    <div class="city-list" ref="wrapper">
        <div>
            <div class="city-area">
                <div class="city-title">
                    当前城市
                </div>
                <div class="city-button-list">
                    <div class="city-button-wrapper">
                        <router-link to="/">
                            <div class="city-button">
                                {{this.$store.state.city}}
                            </div>
                        </router-link>
                    </div>
                </div>
            </div>
            <div class="city-area">
                <div class="city-title">
                    热门城市
                </div>
                <div class="city-button-list">
                    <div class="city-button-wrapper" v-for="item of hotCities" :key="item.id"><div class="city-button" @click="changeCity(item.name)">{{ item.name }}</div></div>
                </div>
            </div>
            <div class="city-area" v-for="(items,key) of cities" :key="key" :ref="key">
                <div class="city-title">
                    {{ key }}
                </div>
                <ul>
                    <li class="city-item" v-for="item of items" :key="item.id" @click="changeCity(item.name)">{{ item.name }}</li>
                </ul>
            </div>
        </div>
    </div>
</template>

<script>
import BScroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        this.scroll.scrollToElement(this.$refs[this.letter][0])
      }
    }
  },
  methods: {
    changeCity (city) {
      this.$store.commit('changeCity', city)
      this.$router.push('/')
    }
  }
}
</script>

<style scoped>
.city-list {
    overflow: hidden;
    position: absolute;
    top: 1.6rem;
    left: 0;
    right: 0;
    bottom: 0;
}
.city-title {
    height: .6rem;
    line-height: .6rem;
    background: #f6f6f6;
    font-size: .24rem;
    padding-left: .2rem;
}
.city-button-list {
    padding: .1rem;
    overflow: hidden;
}
.city-button-wrapper {
    float: left;
    width: 33.33%;
}
.city-button {
    border-radius: .05rem;
    border:.02rem solid #ddd;
    text-align: center;
    margin: .1rem;
    padding: .1rem 0;
    color: #333;
}
.city-item {
    padding:.25rem .2rem;
    border-bottom: .02rem solid #f6f6f6;
}
</style>
